﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using G15_Agent.Models.Hotel;
using System.Net;       

namespace G15_Agent.BUS
{
    public class BUS_Hotel
    {
        //public Service.Hotel_G08.HotelG08ServiceSoapClient serviceG08 { get; set; }
        public Service.Hotel_G08.HotelG08Service serviceG08 { get; set; }
        public Service.Hotel_G11.WebService serviceG11 { get; set; }

        public BUS_Hotel()
        {
            //serviceG08 = new Service.Hotel_G08.HotelG08ServiceSoapClient("HotelG08ServiceSoap");
            serviceG08 = new Service.Hotel_G08.HotelG08Service();
            serviceG11 = new Service.Hotel_G11.WebService();
        }

        #region WSHotel0 - Đăng nhập

        public bool DangNhap_G08(string userName, string password)
        {
            try
            {
                CookieContainer cookieJar = new CookieContainer();
                serviceG08.CookieContainer = cookieJar;
                return serviceG08.DangNhap(userName, password);
            }
            catch (System.Exception ex)
            {
                string logError = ex.ToString();	
            }
            return false;
        }

        public bool DangNhap_G11(string userName, string password)
        {
            try
            {
                CookieContainer cookieJar = new CookieContainer();
                serviceG11.CookieContainer = cookieJar;
                return serviceG11.DangNhap(userName, password);
            }
            catch (System.Exception ex)
            {
                string logError = ex.ToString();
            }
            return false;
        }

        #endregion

        #region WSHotel1 - Lâý danh sách hạng phòng

        public List<HangPhong> LayDanhSachHangPhong_G08()
        {
            List<HangPhong> result = new List<HangPhong>();
            try
            {
                Service.Hotel_G08.HangPhong[] dsHangPhong = serviceG08.LayDSHangPhong();
                foreach (Service.Hotel_G08.HangPhong serverItem in dsHangPhong)
                {
                    HangPhong clientItem = new HangPhong();
                    clientItem.maHangPhong = serverItem.MaHangPhong.ToString().Trim();
                    clientItem.tenHangPhong = serverItem.TenHangPhong.Trim();
                    clientItem.moTa = serverItem.MoTa.Trim();

                    result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            
            if (result.Count == 0) return null;
            return result;
        }

        public List<HangPhong> LayDanhSachHangPhong_G11()
        {
            List<HangPhong> result = new List<HangPhong>();
            try
            {
                Service.Hotel_G11.HangPhong[] dsHangPhong = serviceG11.LayDSHangPhong();
                foreach (Service.Hotel_G11.HangPhong serverItem in dsHangPhong)
                {
                    HangPhong clientItem = new HangPhong();
                    clientItem.maHangPhong = serverItem.maHangPhong.ToString().Trim();
                    clientItem.tenHangPhong = serverItem.tenHangPhong.Trim();
                    clientItem.moTa = "";

                    result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region WSHotel4 - Lây danh sách loại phòng

        public List<LoaiPhong> LayDanhSachLoaiPhong_G08()
        {
            List<LoaiPhong> result = new List<LoaiPhong>();
            try
            {
                Service.Hotel_G08.LoaiPhong[] dsLoaiPhong = serviceG08.LayDSLoaiPhong();
                foreach (Service.Hotel_G08.LoaiPhong serverItem in dsLoaiPhong)
                {
                    LoaiPhong clientItem = new LoaiPhong();
                    clientItem.maLoaiPhong = serverItem.MaLoaiPhong.Trim();
                    clientItem.tenLoaiPhong = serverItem.TenLoaiPhong.Trim();
                    clientItem.soNguoi = serverItem.SoNguoi;
                    clientItem.moTa = serverItem.MoTa;

                    result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            
            if (result.Count == 0) return null;
            return result;
        }

        public List<LoaiPhong> LayDanhSachLoaiPhong_G11()
        {
            List<LoaiPhong> result = new List<LoaiPhong>();
            try
            {
                Service.Hotel_G11.LoaiPhong[] dsLoaiPhong = serviceG11.LayDSLoaiPhong();
                foreach (Service.Hotel_G11.LoaiPhong serverItem in dsLoaiPhong)
                {
                    LoaiPhong clientItem = new LoaiPhong();
                    clientItem.maLoaiPhong = serverItem.maLoaiPhong.Trim();
                    clientItem.tenLoaiPhong = serverItem.tenLoaiPhong.Trim();
                    clientItem.soNguoi = (int)serverItem.soLuong;
                    clientItem.moTa = "";

                    result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        /// <summary>
        ///  Cực nhất
        /// </summary>
        /// <param name="dsPhong"></param>
        /// <param name="ngayBD"></param>
        /// <param name="ngayKT"></param>
        /// <returns></returns>
        #region WSHotel6 - Gưỉ yêu cầu thuê phòng

        public string DatPhong_G08(List<Phong> dsPhong, DateTime ngayBD, DateTime ngayKT)
        {
            G15_Agent.Service.Hotel_G08.LoaiHangPhong[] dsLoaiHangPhong = new G15_Agent.Service.Hotel_G08.LoaiHangPhong[dsPhong.Count];
            for (int i = 0; i < dsPhong.Count; i++)
            {
                dsLoaiHangPhong[i] = new Service.Hotel_G08.LoaiHangPhong();
                dsLoaiHangPhong[i].GiaPhong = (int)dsPhong[i].giaPhong;
                dsLoaiHangPhong[i].MaHangPhong = dsPhong[i].maHangPhong;
                dsLoaiHangPhong[i].MaLoaiPhong = dsPhong[i].maLoaiPhong;
                dsLoaiHangPhong[i].SoLuong = dsPhong[i].soLuong;
            }
            try
            {
                return serviceG08.DatPhong(dsLoaiHangPhong, ngayBD, ngayKT);
            }
            catch (System.Exception ex)
            {
                return null;
            }

        }

        public string DatPhong_G11(List<Phong> dsPhong, DateTime ngayBD, DateTime ngayKT)
        {
            G15_Agent.Service.Hotel_G11.LoaiHangPhong[] dsLoaiHangPhong = new G15_Agent.Service.Hotel_G11.LoaiHangPhong[dsPhong.Count];
            for (int i = 0; i < dsPhong.Count; i++)
            {
                dsLoaiHangPhong[i] = new Service.Hotel_G11.LoaiHangPhong();
                dsLoaiHangPhong[i].gia = (int)dsPhong[i].giaPhong;  // null cũng đc
                dsLoaiHangPhong[i].maHangPhong = dsPhong[i].maHangPhong;
                dsLoaiHangPhong[i].maLoaiPhong = dsPhong[i].maLoaiPhong;
                dsLoaiHangPhong[i].soLuong = dsPhong[i].soLuong;
            }
            try
            {
                return serviceG11.DatPhong(dsLoaiHangPhong, ngayBD, ngayKT, 1);     // Số 0 do service viết dư mà làm biếng xóa, biến đó ko xài
            }
            catch (System.Exception ex)
            {
                return null;
            }

        }


        #endregion

        #region WSHotel7 - Tìm kiếm chính xác theo loại, hạng, ngày đến

        public List<Phong> TimKiemPhongTheoTieuChi_G08(string maHangPhong, string maLoaiPhong, DateTime ngayDen, DateTime ngayDi)
        {
            List<Phong> result = new List<Phong>();

            if (ngayDi == null || ngayDi == ngayDen)
                ngayDi = ngayDen.AddDays(2);
            try
            {
                Service.Hotel_G08.LoaiHangPhong[] dsPhong = serviceG08.LayDSLoaiHangPhongHoTro(ngayDen, ngayDi);
                foreach (Service.Hotel_G08.LoaiHangPhong serverItem in dsPhong)
                {
                    Phong clientItem = new Phong();
                    clientItem.maHangPhong = serverItem.MaHangPhong.Trim();
                    clientItem.maLoaiPhong = serverItem.MaLoaiPhong.Trim();
                    clientItem.giaPhong = serverItem.GiaPhong;
                    clientItem.soLuong = serverItem.SoLuong;

                    if (serviceG08.LaySoLuongPhongTrong(clientItem.maLoaiPhong, clientItem.maHangPhong, ngayDen, ngayDi) > 0)
                        result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            
            if (result.Count == 0) return null;
            return result;
        }

        public List<Phong> TimKiemPhongTheoTieuChi_G11(string maHangPhong, string maLoaiPhong, DateTime ngayDen, DateTime ngayDi)
        {
            List<Phong> result = new List<Phong>();

            if (ngayDi == null || ngayDi == ngayDen)
                ngayDi = ngayDen.AddDays(2);
            try
            {
                Service.Hotel_G11.LoaiHangPhong[] dsPhong = serviceG11.LayDSLoaiHangPhongHoTro(ngayDen, ngayDi);
                foreach (Service.Hotel_G11.LoaiHangPhong serverItem in dsPhong)
                {
                    Phong clientItem = new Phong();
                    clientItem.maHangPhong = serverItem.maHangPhong.Trim();
                    clientItem.maLoaiPhong = serverItem.maLoaiPhong.Trim();
                    clientItem.giaPhong = (double)serverItem.gia;
                    clientItem.soLuong = (int)serverItem.soLuong;

                    if (((maHangPhong == "null" || (maHangPhong != "null" && clientItem.maHangPhong == maHangPhong))
                       && (maLoaiPhong == "null" || (maLoaiPhong != "null" && clientItem.maLoaiPhong == maLoaiPhong)))
                       && clientItem.soLuong > 0)
                        result.Add(clientItem);

                    if (serviceG11.LaySoLuongPhongTrong(clientItem.maLoaiPhong, clientItem.maHangPhong, ngayDen, ngayDi) > 0)
                        result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region WSHotel8 - Tìm kiếm phong

        public List<Phong> TimKiemPhong_G08(string maHangPhong, string maLoaiPhong, DateTime ngayDen, double giaToiThieu, double giaCaoNhat)
        {
            List<Phong> result = new List<Phong>();

            DateTime ngayDi = ngayDen.AddDays(2);
            try
            {
                Service.Hotel_G08.LoaiHangPhong[] dsPhong = serviceG08.LayDSLoaiHangPhongHoTro(ngayDen, ngayDi);
                foreach (Service.Hotel_G08.LoaiHangPhong serverItem in dsPhong)
                {
                    Phong clientItem = new Phong();
                    clientItem.maHangPhong = serverItem.MaHangPhong.Trim();
                    clientItem.maLoaiPhong = serverItem.MaLoaiPhong.Trim();
                    clientItem.giaPhong = serverItem.GiaPhong;
                    clientItem.soLuong = serverItem.SoLuong;

                    if (serviceG08.LaySoLuongPhongTrong(clientItem.maLoaiPhong, clientItem.maHangPhong, ngayDen, ngayDi) > 0
                        && clientItem.giaPhong > giaToiThieu && clientItem.giaPhong < giaCaoNhat)
                        result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
           
            if (result.Count == 0) return null;
            return result;
        }

        public List<Phong> TimKiemPhong_G11(string maHangPhong, string maLoaiPhong, DateTime ngayDen, DateTime ngayDi, double giaToiThieu, double giaCaoNhat)
        {
            List<Phong> result = new List<Phong>();

            if (ngayDi == null)
                ngayDi = ngayDen.AddDays(2);
            try
            {
                Service.Hotel_G11.LoaiHangPhong[] dsPhong = serviceG11.LayDSLoaiHangPhongHoTro(ngayDen, ngayDi);
                foreach (Service.Hotel_G11.LoaiHangPhong serverItem in dsPhong)
                {
                    Phong clientItem = new Phong();
                    clientItem.maHangPhong = serverItem.maHangPhong.Trim();
                    clientItem.maLoaiPhong = serverItem.maLoaiPhong.Trim();
                    clientItem.giaPhong = (double)serverItem.gia;
                    clientItem.soLuong = (int)serverItem.soLuong;

                    if (serviceG11.LaySoLuongPhongTrong(clientItem.maLoaiPhong, clientItem.maHangPhong, ngayDen, ngayDi) > 0
                        && clientItem.giaPhong > giaToiThieu && clientItem.giaPhong < giaCaoNhat)
                        result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

    }
}